// pages/mall/index.js
const app = getApp();
// import trigger from '../../utils/util.js'
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		load_img:'//resource.160nurse.com/canvas/healthy/img/common/load.gif',
		scroll_true:'',
		resourceDomain:app.resourceDomain,
		sou_list:[],
		nav_on_2:'on'
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		var that = this;
		this.luo_suo = true;
		this.index_num = 1;
		this.scroll_suo = false;
		//授权
		// app.checkSession(this,'../mall/index');

		//分类和新人推荐
    app.request(app.apiDomain + "index.php?c=mall&a=home_cat_list", {}, function(res) {
			// console.log(res.data.data)
			that.setData({
				new_class: res.data.data,
			});
		});

		//健康币兑换//助力省钱购
		app.request(app.apiDomain + "index.php?c=mall&a=goodslist", {}, function(res) {
			// console.log(res.data.data)
			that.setData({
				Match_help: res.data.data,
			});
		});

		//每日必看
		app.request(app.apiDomain + "index.php?c=mall&a=exchangegoodslist", {
			page: 1,
			size: 10
		}, function(res) {
			// console.log(res.data.data)
			that.setData({
				Everyday: res.data.data
			});
		});
		
		//公告
		app.request(app.apiDomain + "index.php?c=mall&a=notice", {
			type:'shop'
		}, function (res) {
			// console.log(res.data.data)
			that.setData({
				gao:res.data.data
			});
		});
		
		//自助福利
		// app.request(app.apiDomain + "index.php?c=lottery&a=goodsList", {
		// 	size:8
		// }, function (res) {
		// 	// console.log(res.data.data)
		//   that.setData({
		// 		free:res.data.data
		//   });
		// });
		
		//banner
		app.request(app.apiDomain + "index.php?c=mall&a=banner", {
			type:2,
		}, function (res) {
			// console.log(res.data.data)
			that.setData({
				banner:res.data.data
			});
		});
		//搜索记录
		if(wx.getStorageSync('sou_list')){
			that.setData({
				sou_list:wx.getStorageSync('sou_list')
			});
			// console.log('把缓存放都data来',that.data.sou_list)
		}
		
		

		//滚动条
		var num = 0;
		const vm = this;
		var off = true;
		app.request(app.apiDomain + "index.php?c=mall&a=lastestorderlist", {}, function(res) {
// 			if (res.data.status == '0') {
// 				wx.navigateTo({
// 					url: '../logs/accredit?jump=../mall/index'
// 				})
// 			}
			that.setData({
				lastestorderlist: res.data.data
			});
			if (vm.data.lastestorderlist) {
				if (vm.data.lastestorderlist.length > 1) {
					setInterval(function() {
						if (off) {
							num++
							vm.setData({
								top_num: '-' + 7 * num + 'vw'
							})
							if (num == (vm.data.lastestorderlist.length - 1)) {
								off = false;
							}
						} else {
							num--
							vm.setData({
								top_num: '-' + 7 * num + 'vw'
							})
							if (num == 0) {
								off = true;
							}
						}
					}, 3000);
				}
			}
		});
		// 抢购
		// app.request(app.apiDomain + "index.php?c=mall&a=qianggou", {
		// 	size:3
		// }, function(res) {
		// 	that.setData({
		// 	    time_list: res.data.data
		// 	})
		// });
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {
		app.SystemInfo(this)
	},
	gao_click:function(){
		this.setData({
			gao_on:'on'
		})
	},
  getUserInfo: function () {
    var that = this
    if (app.getUserInfo(that)) {
      that.setData({
        user_auth: false
      })
    }
  },
	gao_off:function(){
		this.setData({
			gao_on:''
		})
	},
	scroll: function() {
		var thas = this;
		if (this.luo_suo) {
			var query = wx.createSelectorQuery();
			query.select('#box').scrollOffset(function(res) {
				if (res.scrollTop >= 500) {
					// console.log('加载图片')
					thas.setData({
						img_suo: true
					})
					thas.luo_suo = false;
				}
			}).exec();
		}
	},
	//下拉加载
	scroll_get: function() {
		if (this.scroll_suo) {return false}
		var that = this;
		this.setData({
			scroll_true:'on'
		})
		this.index_num++
		// console.log(this.index_num)
		app.request(app.apiDomain + "index.php?c=mall&a=exchangegoodslist", {
			page: that.index_num,
			size: 10,
		}, function(res) {
			that.setData({
				scroll_true:''
			});
			if (res.data.data == '[]' || res.data.data.length == 0) {
				wx.showToast({
					title: '已经到底了',
					icon: 'none',
					duration: 1000
				})
				that.scroll_suo = true;
			} else {
				that.setData({
					Everyday: that.data.Everyday.concat(res.data.data)
				})
			}
		});
	},
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
    // if ((trigger.trigger)(this,'../mall/index')) {
    //   this.onLoad()
    // }
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	sou_click:function(){
		var thas=this;
		this.setData({
			sou_suo:'on'
		})
		setTimeout(function(e=thas){
			e.setData({
				focus:true
			})
		},300)
	},
	sou_off:function(){
		this.setData({
			sou_suo:''
		})
	},
	sou_li_click:function(e){
		var name = e.currentTarget.dataset.words;
		this.setData({
			sou_suo:''
		})
		// console.log(name)
		wx.navigateTo({
			url:'list?sou_suo=yes&f_id=105103102101&type=exchange&title=' + name
		})
	},
	sou_suo_get:function(e){
		var name = e.detail.value;
		var detail = this.data.sou_list;
		// console.log(name,detail,'click')
		detail.unshift(name)
		this.setData({
			sou_list:detail,
			sou_suo:''
		});
		wx.setStorage({
			key: "sou_list",
			data: detail ,
		})
		// console.log(wx.getStorageSync('sou_list'),this.data.sou_list,'点击后')
		wx.navigateTo({
			url:'list?sou_suo=yes&f_id=105103102101&type=exchange&title=' + name
		})
	},
	float_3_click:function(){
		wx.showToast({
		  title: '趣味健康活动升级中，敬请期待。',
		  icon: 'none',
		  duration: 2000
		})
	},
})
